# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可

    # 需要枚举子串，判断是否为回文，应该用动态规划
    def longestPalindrome(self, s: str) -> str:
        str_len = len(s)
        ans = ""
        if str_len < 2:
            return s
        elif str_len == 2:
            if s[0] == s[1]:
                return s
            else:
                return s[0]
        else:
            ans = self.get_longest_palindrome(s)
            if len(ans) == 0:
                ans = s[0]
        return ans

    # 只需要处理长度从2到n的子串，动态规划解法
    # 复杂度：O(n的平方)
    def get_longest_palindrome(self, s: str) -> str:
        str_len = len(s)

        # 生成dp[][]数组
        dp = [[False] * str_len for _ in range(str_len)]
        for i in range(str_len):
            dp[i][i] = True

        max_len = 0
        start_index = 0
        for sub_str_len in range(2, str_len + 1):
            for i in range(str_len):
                j = sub_str_len + i - 1
                if j >= str_len:
                     break

                if s[i] != s[j]:
                    dp[i][j] = False
                else:
                    if j - i < 3:
                        dp[i][j] = True
                    else:
                        dp[i][j] = dp[i+1][j-1]

                if dp[i][j] and sub_str_len > max_len:
                    max_len = sub_str_len
                    start_index = i

        return s[start_index: start_index + max_len]

    # 暴力解，复杂度是O(n的立方）
    def longestPalindromeForce(self, s: str) -> str:
        str_len = len(s)
        max_len = 0
        ans = ""

        if str_len == 1:
            return s

        for i in range(str_len):
            for j in range(i, str_len):
                temp = s[i:j+1]
                temp_len = self.get_palindrome_len(temp)
                if temp_len > max_len:
                    max_len = temp_len
                    ans = temp
        return ans

    # O(n)
    def get_palindrome_len(self, s: str) -> int:
        str_len = len(s)
        mid_len = (str_len + 1) >> 1
        if str_len == 1:
            return 1

        for i in range(mid_len):
            if s[i] != s[str_len - i - 1]:
                return 0

        return str_len


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    testcases = [
        # 使用元组来保存输入的数据，期待的结果
        ("babad", "bab"),
        ("cbbd", "bb"),
        ("a", "a"),
        ("ac", "a"),
        ("bb", "bb"),
        ("abcda", "a")
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        # TODO: 单独一条的Test case的组成随题目的不同而改变，需要按照题目来修改代码
        input1 = case_item[0]
        expect = case_item[len(case_item) - 1]
        # TODO: 调用对应方法，需要替换具体的方法
        exec_result = solution.longestPalindrome(input1)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

